package com.dingding.web.coupon;

import java.util.List;

import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.tools.wechat.WechatTool;
import com.dingding.facde.activity.dto.ReceiveCouponDTO;
import com.dingding.facde.activity.dto.ShowUserCouponDTO;
import com.dingding.facde.coupon.ICouponService;
import com.dingding.web.base.action.BaseAction;


/**
 * 
 * @ClassName: CarWashCouponAction
 * @Description: 现金优惠券action
 * @author fengshuonan
 * @date 2016年2月4日 上午10:30:12
 * @version 叮叮搭车2.0
 * 
 */
@SuppressWarnings("serial")
public class CouponAction extends BaseAction {
	
	/**
	 * @Title: startReceiveCoupon
	 * @Description: 跳转到领取优惠券页面
	 * @author fengshuonan
	 * @return String
	 * @throws
	 */
	public String startReceiveCoupon()
	{
		//获取微信跳转的code
		String code = getPara("code");
		
		//获取用户优惠券类型和优惠券金额
		String couponType = getPara("couponType");
		String couponMoney = getPara("couponMoney");
		
		//获取时哪家推广机构的链接
		String advId = getPara("advId");
		
		//让该推广商的点击次数加1
		super.couponService.addAdvertiseNum(advId, 1);
		
		//根据code获取微信openId
		String openId = WechatTool.getOpenIDByCode(code);
		
		//根据openId和手机号判断是否注册过
		boolean isReg = super.couponService.isExistOpenId(openId);
		
		//生成js-sdk所需要的变量
		super.validation = super.getWechatValidation(super.request);
		
		setPara("couponType", couponType); //设置优惠券类型
		setPara("couponMoney", couponMoney);//优惠券金额
		setPara("openId", openId);//用户的微信id
		setPara("advId", advId); //推广商的主键id

		//返回推广商id
		setPara("advId", advId);
		
		//微信公众号没有和手机号绑定
		if(!isReg){
			return "receivecoupon";
		}
		
		//设置为已激活
		setPara("isActive","true");
		
		//如果是出行券
		if("2".equals(couponType)){
			return "receivecouponcx";
		}else{
			//如果是洗车券
			return "receivecouponxc";
		}
		
	}
	
	/**
	* @Title: getUserCouponList
	* @Description: 根据用户ID获取用户优惠券列表
	* @author CHEHAUNBO
	*/
	public String getUserCouponList() {

		// 用户ID
		String userId = getPara("userId");
		if (ToolUtil.isBlank(userId)) {
			super.generateResponseData(300, "用户ID为空！", null);
		} else {
			// 根据用户ID获取用户所有优惠券
			List<ShowUserCouponDTO> coupons = couponService.getUserCouponList(userId);
			// 生成响应数据
			String resJosn = super.generateResponseData(200, null, coupons);
			super.write(resJosn);
//			System.out.println(resJosn);
		}

		return NONE;

	}

	/**
	* @Title: getVerifyCode
	* @Description:获取验证码
	* @author fengshuonan
	* @return String
	* @throws
	*/
	public String getVerifyCode() {

		String tellphone = getPara("tellphone");
		
		super.phonevalidateService.getVerifyCode(tellphone);
		
		return NONE;

	}

	/**
	*
	* @Title: getCashCoupon
	* @Description: 领取现金优惠券
	* @author fengshuonan
	* @return String
	* @throws
	*
	*/
	public String getCashCoupon() {

		String tellphone = getPara("tellphone");
		String verifyCode = getPara("verifyCode");

		//生成js-sdk所需要的变量
		super.validation = super.getWechatValidation(super.request);

		ReceiveCouponDTO receiveCouponDTO = new ReceiveCouponDTO();
		receiveCouponDTO.setTellPhone(tellphone);
		receiveCouponDTO.setVaidateNum(verifyCode);
		receiveCouponDTO.setCouponType(getPara("couponType"));
		receiveCouponDTO.setWeChatId(getPara("openId"));
		
		//生成优惠券（状态码：1：验证码错误  2：请求成功）
		int statusCode = super.couponService.generateCoupon(receiveCouponDTO);
		
		if(statusCode == 1){
			setPara("errorMsg", "wrong_verify_code");
			return "receivecoupon";//如果验证码不成功还返回首页
		}
		
		//激活优惠券
		boolean flag = super.couponService.activeCouponByTel(tellphone);
		
		if(flag){
			setPara("isActive","true");
		}
		
		//让该推广结构的注册次数加1
		super.couponService.addAdvertiseNum(getPara("advId"), 3);
		
		//返回推广商id
		setPara("advId", getPara("advId"));
		
		//返回给前台优惠券类型
		setPara("couponType", getPara("couponType"));
		
		//返回给前台金额
		setPara("couponMoney", getPara("couponMoney"));
		
		//返回前台手机号码
		setPara("tellphone", tellphone);
		
		//如果是出行券
		if("2".equals(getPara("couponType"))){
			return "chuxingquanPage";
		}else{
			return "xichequanPage";
		}
	}
	
	
	/**
	 * @Title: activateCoupon
	 * @Description: 激活优惠券
	 * @author fengshuonan
	 * @return String
	 * @throws
	 */
	public String activateCoupon()
	{
		boolean flag = super.couponService.activeCouponByTel(getPara("tellphone"));
		
		log.info("tell=" + getPara("tellphone") + ",flag=" + flag);
		
		if(flag==true){
			super.write("success");
		}else{
			super.write("fail");
		}
		
		return NONE;
	}
	
	
	/**
	 * @Title: activeAppCoupon
	 * @Description: app端通过id激活优惠券
	 * @author fengshuonan
	 * @return String
	 * @throws
	 */
	public String activeAppCoupon()
	{
		boolean flag = super.couponService.activateCoupon(getPara("couponId"));
		
		String resJosn = super.generateResponseData(200, null, flag);
		
		super.write(resJosn);
		
		return NONE;
	}
	
	
	/**
	 * @Title: addShareNum
	 * @Description: 增加推广商链接的分享次数
	 * @author fengshuonan
	 * @return String
	 * @throws
	 */
	public String addShareNum(){
		
		//推广商的主键id
		String advId = getPara("advId");
		
		//让该推广结构的分享次数加1
		super.couponService.addAdvertiseNum(advId, 2);
		
		return NONE;
	}
	
	
	public ICouponService getCouponService() {
		return couponService;
	}

	public void setCouponService(ICouponService couponService) {
		this.couponService = couponService;
	}

}
